<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<!-- upper-lower vars for use in case-insensitive sorting -->
	<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
	<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

	<!--
	date and time functions
	requires dates to be in W3C format: 2005-08-15T15:52:01+00:00
	-->

	<xsl:template name="format-timestamp">
		<xsl:param name="timestamp" select="''"/>
		<xsl:param name="part" select="''"/>
		<xsl:param name="format" select="'-'"/>
		<xsl:choose>
			<xsl:when test="contains($timestamp,',')">
				<!-- unsupported format, just return the date without messing with it -->
				<xsl:value-of select="$timestamp"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<!-- show a full timestamp -->
					<xsl:when test="$part = ''">
						<xsl:call-template name="format-date">
							<xsl:with-param name="timestamp" select="$timestamp"/>
							<xsl:with-param name="format" select="$format"/>
						</xsl:call-template>
						<xsl:text> </xsl:text>
						<xsl:call-template name="format-time">
							<xsl:with-param name="timestamp" select="$timestamp"/>
						</xsl:call-template>
					</xsl:when>
					<!-- show just the date part -->
					<xsl:when test="$part = 'timestamp'">
						<xsl:call-template name="format-date">
							<xsl:with-param name="timestamp" select="$timestamp"/>
							<xsl:with-param name="format" select="$format"/>
						</xsl:call-template>
					</xsl:when>
					<!-- show just the time part -->
					<xsl:when test="$part = 'Time'">
						<xsl:call-template name="format-time">
							<xsl:with-param name="timestamp" select="$timestamp"/>
						</xsl:call-template>
					</xsl:when>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="format-date">
		<xsl:param name="timestamp" select="''"/>
		<xsl:param name="format" select="'-'"/>
		<xsl:choose>
			<xsl:when test="string-length($timestamp) &gt; 1">
				<xsl:choose>
					<xsl:when test="string-length($timestamp) = 14">
						<!-- ISO date length -->
						<xsl:variable name="Year" select="substring($timestamp,1,4)"/>
						<xsl:variable name="Month" select="substring($timestamp,6,2)"/>
						<xsl:variable name="Day" select="substring($timestamp,9,2)"/>
						<xsl:choose>
							<xsl:when test="$format = '/'">
								<xsl:value-of select="concat($Month,'/',$Day,'/',$Year)"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="concat($Year,'-',$Month,'-',$Day)"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<!-- if not, we'll assume it has dashes -->
						<xsl:variable name="Year" select="substring($timestamp,1,4)"/>
						<xsl:variable name="Month" select="substring($timestamp,6,2)"/>
						<xsl:variable name="Day" select="substring($timestamp,9,2)"/>
						<xsl:choose>
							<xsl:when test="$format = '/'">
								<xsl:value-of select="concat($Month,'/',$Day,'/',$Year)"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="concat($Year,'-',$Month,'-',$Day)"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text> </xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="format-time">
		<xsl:param name="timestamp" select="''"/>
		<xsl:if test="$timestamp">
			<xsl:variable name="Hour" select="substring($timestamp,12,2)*1"/>
			<xsl:variable name="Minutes" select="substring($timestamp,15,2)"/>
			<xsl:choose>
				<xsl:when test="$Hour &gt; 12">
					<xsl:value-of select="concat(number($Hour) - 12,':',$Minutes,' PM')"/>
				</xsl:when>
				<xsl:when test="$Hour &gt; 11">
					<xsl:value-of select="concat($Hour,':',$Minutes,' PM')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat($Hour,':',$Minutes,' AM')"/>
				</xsl:otherwise>
			</xsl:choose>

		</xsl:if>
	</xsl:template>

</xsl:stylesheet>
